package appUtils;

import android.Manifest;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.os.CancellationSignal;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;

import com.example.jhlibrarys.R;

import static android.content.Context.FINGERPRINT_SERVICE;

/**
 * 指纹工具类
 * Created by Administrator on 2017/7/13.
 */

public final class FingerprintUtil {
    /**
     * 检查手机是否有屏幕锁
     *
     * @param context
     * @return
     */
    protected static boolean isKeyguardSecure(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
            return keyguardManager.isKeyguardSecure();
        } else {
            ToastUtil.showShort(R.string.android_version_not_support_fingerprint);
            return false;
        }
    }

    /**
     * 判断指纹功能是否可以使用
     *
     * @param context
     * @return
     */
    public static boolean getFingerprintEnAble(Context context) {
        boolean enAble = false;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            android.hardware.fingerprint.FingerprintManager manager = (android.hardware.fingerprint.FingerprintManager) context.getSystemService(FINGERPRINT_SERVICE);
            if (manager != null && ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) == PackageManager.PERMISSION_GRANTED) {
                enAble = manager.hasEnrolledFingerprints() && manager.isHardwareDetected() && isKeyguardSecure(context);
            }
            return enAble;
        } else {
            ToastUtil.showShort(R.string.android_version_not_support_fingerprint);
            return false;
        }
    }

    /**
     * 使用反射检查是否存在对应的所要使用的类
     *
     * @param className
     * @return
     */
    private static boolean checkFingerItme(String className) {
        try {
            Class.forName(className);
            LogUtil.log(className + " = true");
            return true;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            LogUtil.log(className + " = false");
            PayTokenException.getInstance().saveExceptionFile(className + " = false error: "
                    + e.getMessage());
            return false;
        }
    }

    /**
     * 手机是否存在指纹解锁所需要的类
     *
     * @return
     */
    public static boolean hasFingerprint() {
        return checkFingerItme("android.hardware.fingerprint.FingerprintManager");
    }

    /**
     * 唤醒指纹解锁
     *
     * @param context
     * @param crypto
     * @param cancel·
     * @param flags
     * @param callback
     * @param handler
     */
    public static void authenticate(Context context, @Nullable FingerprintManager.CryptoObject crypto, @Nullable CancellationSignal cancel,
                                    int flags, @NonNull FingerprintManager.AuthenticationCallback callback, @Nullable Handler handler) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            android.hardware.fingerprint.FingerprintManager manager = (android.hardware.fingerprint.FingerprintManager) context.getSystemService(FINGERPRINT_SERVICE);
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) == PackageManager.PERMISSION_GRANTED) {
                if (manager.hasEnrolledFingerprints() && manager.isHardwareDetected() && FingerprintUtil.isKeyguardSecure(context)) {
                    manager.authenticate(crypto, cancel, flags, callback, handler);

                }
            }
        } else {
            ToastUtil.showShort(R.string.android_version_not_support_fingerprint);
        }
    }
}
